import json
import csv
import pandas as pd

filename = "C:/Users/yinwe/Desktop/data.json"

with open(filename, 'r', encoding='UTF-8') as file_in:
    lines = file_in.readlines()

# 获取所有的键
keys = []
for line in lines:
    data = json.loads(line)
    # 将每个字典的键添加到列表中，避免重复
    keys.extend(data.keys())

# 去除重复的键，并按原顺序排序
keys = sorted(set(keys))

with open("data.csv", mode='w', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=keys)
    # 写入header
    writer.writeheader()
    # 写入数据
    for line in lines:
        data = json.loads(line)
        writer.writerow(data)
